package cn.itcast.nems.config;

import cn.itcast.nems.common.web.filter.ExceptionFilter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.List;

/**
 * 异常过滤器配置
 */
@Configuration
public class FilterConfig {

    private Logger logger = LoggerFactory.getLogger(this.getClass());

    /**
     * 全局异常处理
     *
     * @return
     */
    @Bean
    @ConditionalOnProperty(name = "exeption.filter", matchIfMissing = true)
    @ConditionalOnMissingBean(ExceptionFilter.class)
    public FilterRegistrationBean exceptionFilter() {
        logger.info("Create global excepiton filter");
        ExceptionFilter exceptionFilter = new ExceptionFilter();
        FilterRegistrationBean<ExceptionFilter> registration = new FilterRegistrationBean<ExceptionFilter>();
        registration.setUrlPatterns(List.of("/*"));
        registration.setFilter(exceptionFilter);
        registration.setOrder(Integer.MIN_VALUE + 5);
        return registration;
    }

}
